# Maintainer: Jianfeng Liu <liujianfeng1994@gmail.com>
# Contributor: Olivier Michel <Olivier.Michel@cyberbotics.com>
# Contributor: Andrew Sun <adsun701@gmail.com>

_realname=libzip
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.8.0
pkgrel=2
pkgdesc="A C library for reading, creating, and modifying zip archives (mingw-w64)"
url="https://libzip.org"
license=('BSD')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-nettle"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('staticlibs' '!libtool')
source=("https://libzip.org/download/${_realname}-${pkgver}.tar.xz"
        "https://github.com/nih-at/libzip/commit/ca61524.patch")
sha512sums=('fe94a26a2797dff7c9b27334f29982f8e523dd5095bf32ebcadc6443726864f0ff7b2e8bf243e52cad8f79297bc7a87d73fdbc35edae4ffc8ab6aa0bcc93b03e'
            'c472245056b189dcb9c8b9b17c75f663b439e17449d4b0d6d494db981054f25a9bb25dbb0a0c924bdbbaaf9ec51334080d95f94248b108fa9eb8475335ab9c3d')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # https://github.com/nih-at/libzip/pull/244
  patch -p1 -i "${srcdir}/ca61524.patch"
}

build() {
  # Shared Build
  [[ -d ${srcdir}/build-${CARCH}-shared ]] && rm -rf ${srcdir}/build-${CARCH}-shared
  mkdir -p ${srcdir}/build-${CARCH}-shared && cd ${srcdir}/build-${CARCH}-shared

  cd "${srcdir}/build-${CARCH}-shared"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DENABLE_COMMONCRYPTO=OFF \
    -DENABLE_GNUTLS=ON \
    -DENABLE_OPENSSL=OFF \
    -DENABLE_MBEDTLS=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./

  # Static Build
  [[ -d ${srcdir}/build-${CARCH}-static ]] && rm -rf ${srcdir}/build-${CARCH}-static
  mkdir -p ${srcdir}/build-${CARCH}-static && cd ${srcdir}/build-${CARCH}-static

  cd "${srcdir}/build-${CARCH}-static"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=OFF \
    -DENABLE_COMMONCRYPTO=OFF \
    -DENABLE_GNUTLS=ON \
    -DENABLE_OPENSSL=OFF \
    -DENABLE_MBEDTLS=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  # Static Install
  cd "${srcdir}/build-${CARCH}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  # Shared Install
  cd "${srcdir}/build-${CARCH}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
